<template>
  <el-dialog
    :title="!dataForm.brandId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="品牌名" prop="name">
      <el-input v-model="dataForm.name" placeholder="品牌名"></el-input>
    </el-form-item>
    <el-form-item label="品牌logo地址" prop="logo">
      <!-- <el-input v-model="dataForm.logo" placeholder="品牌logo地址"></el-input> -->
      <single-upload v-model="dataForm.logo"></single-upload>
    </el-form-item>
    <el-form-item label="介绍" prop="descript">
      <el-input v-model="dataForm.descript" placeholder="介绍"></el-input>
    </el-form-item>
    <el-form-item prop="showStatus" label="显示状态">
          <el-switch
            v-model="dataForm.showStatus"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="0"
            :inactive-value="1"
          ></el-switch>
      </el-form-item>
    <el-form-item label="检索首字母" prop="firstLetter">
      <el-input v-model="dataForm.firstLetter" placeholder="检索首字母"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import SingleUpload from '../../../components/upload/singleUpload.vue'
import { getBrandById, saveBrand, updateBrand } from '../../../api/product/brand'
  export default {
    components:{
      SingleUpload
    },
    data () {
      return {
        visible: false,
        dataForm: {
          brandId: 0,
          name: '',
          logo: '',
          descript: '',
          showStatus: 0,//默认0未删除
          firstLetter: '',
          sort: ''
        },
        result:'',
        dataRule: {
          name: [
            { required: true, message: '品牌名不能为空', trigger: 'blur' }
          ],
          logo: [
            { required: true, message: '品牌logo地址不能为空', trigger: 'blur' }
          ],
          descript: [
            { required: true, message: '介绍不能为空', trigger: 'blur' }
          ],
          // showStatus: [
          //   { required: true, message: '显示状态[0-不显示；1-显示]不能为空', trigger: 'blur' }
          // ],
          firstLetter: [
            {
            validator: (rule, value, callback) => {
              if (value == "") {
                callback(new Error("首字母不能为空"));
              } else if (!/^[a-zA-Z]$/.test(value)) {
                callback(new Error("首字母为a-z或者A-Z之间的字母"));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
          ],
          sort: [
            {
            validator: (rule, value, callback) => {
              if (value == "") {
                callback(new Error("排序字段不能为空"));
              } else if (!/^[0-9]{1}$/.test(value)) {
                callback(new Error("排序为0~9的整数"));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.brandId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.brandId) {
            // this.$http({
            //   url: this.$http.adornUrl(`/product/product/brand/info/${this.dataForm.brandId}`),
            //   method: 'get',
            //   params: this.$http.adornParams()
            // })
            getBrandById(this.dataForm.brandId)
            .then(({data}) => {
              if (data && data.code === 200) {
                this.dataForm.name = data.data.name
                this.dataForm.logo = data.data.logo
                this.dataForm.descript = data.data.descript
                //状态就不显示了
                this.dataForm.showStatus = data.data.showStatus
                this.dataForm.firstLetter = data.data.firstLetter
                this.dataForm.sort = data.data.sort
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // this.$http({
            //   url: this.$http.adornUrl(`/product/product/brand/${!this.dataForm.brandId ? 'save' : 'update'}`),
            //   method: 'post',
            //   data: this.$http.adornData({
            //     'brandId': this.dataForm.brandId || undefined,
            //     'name': this.dataForm.name,
            //     'logo': this.dataForm.logo,
            //     'descript': this.dataForm.descript,
            //     'showStatus': this.dataForm.showStatus,
            //     'firstLetter': this.dataForm.firstLetter,
            //     'sort': this.dataForm.sort
            //   })
            // })
            if(this.dataForm.brandId){
              console.log(this.dataForm.brandId)
              const updateResult = updateBrand(this.dataForm)
              console.log(updateResult)
              this.result = updateResult
            }else{
              const saveResult = saveBrand(this.dataForm)
              console.log(saveResult)
              this.result = saveResult
            }
            this.result.then(({data}) => {
              console.log("新增or修改")
              console.log(data)
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
